package client;

import handler.ChatClientInitializer;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

@Data
@AllArgsConstructor
public class ChatClient {
    private final String host;
    private final Integer port;

    public void run() throws Exception{
        NioEventLoopGroup loopGroup = new NioEventLoopGroup();

        try{

            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(loopGroup)
                    .channel(NioSocketChannel.class)
                    .handler(new ChatClientInitializer());
            Channel channel = bootstrap.connect("127.0.0.1", 8001).sync().channel();
            Scanner input = new Scanner(System.in);
            String str;
            while (!(str=input.nextLine()).equals("end")){
                channel.writeAndFlush(str + "\r\n");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            loopGroup.shutdownGracefully();
        }
    }
}
